<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{

    public $controller_id;
    public $action_id;
    public $pageTitles;
    public $pageTitle;
    public $user_id;
    public $token;
    public $referer;
    public $dist_host;
    public $active_nav = 0;
    public $rule =array();

    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout='main';
    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu=array();
    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs=array();


    public function beforeAction($action)
    {
        parent::beforeAction($action);
        $this->user_id = Yii::app()->user->getId();
        $user = User::model()->getBySid($this->user_id);
        if(!empty($this->user_id)) {
        if($user['errno']==0){
            if($user['result']['status']==2){
                Yii::app()->user->logout(false);
                $this->showMessage('账户被禁用',$this->createUrl('/user/login'));
                return false;
            }
        }else{
            Yii::app()->user->logout(false);
            $this->showMessage('信息不存在',$this->createUrl('/user/login'));
            return false;
        }
        }
   
        $this->referer = Yii::app()->request->getUrlReferrer();

        $this->controller_id = $this->getId();
        $this->action_id = $this->getAction()->getId();
        if ($this->controller_id =='site' && $this->action_id =='error') {
            return true;
        }
       
        //$this->visitCheck();
        return true;
    }
    protected function afterAction($action)
    {
        $message = Yii::app()->user->getFlash('alertmsg');
        if($message!=null)
        {
            if($message['type'])
                echo '<script>alert("'.$message['msg'].'");</script>';
            else
                echo '<script>alert("'.$message['msg'].'");</script>';
        }
    }

  /**
     * 校验访问权限
     * @return [type] [description]
     */
    private function visitCheck()
    {
        $this->rule['guest'] = array(
            'user'=>array('login', 'logout','signup'),
            'site'=>array('verify', 'error','index'),
            'test'=>array('updownimg'),

        );
        $this->rule['should_login'] =array(
            'user'=>array('info','updateuserinfo','applycarrye'),
            'site'=> array(),
            'address'=> array('list','add','setdefault','edit','deleted'),
            'files' =>array(),
            'message' =>array(),
            'sms' =>array(),
            );
        // if(Yii::app()->user->getIsGuest()){
        //     if(empty($this->rule['guest'][$this->controller_id]) || !in_array($this->action_id,$this->rule['guest'][$this->controller_id])) {
        //        $this->showMessage('请先登录',$this->createUrl('/user/login'));
        //     }
        // }

        if(Yii::app()->user->getIsGuest()){

            if(!empty($this->rule['should_login'])&&in_array($this->action_id,$this->rule['should_login'][$this->controller_id])){
                $this->showMessage('请先登录',$this->createUrl('/user/login'));
                return false;
            }
        }

        //
    }

    public function actions() {  
      
            return array(  
                'captcha' => array(  
                    'class' => 'system.web.widgets.captcha.CCaptchaAction',
                    'backColor' => 0xFFFFFF,
                    'maxLength' => 6,
                    'minLength' => 6,
                    'transparent' => true,
                    'width' => 160,
                    'height' => 40,
                )  
            );  
        }  

    public function checkParams($params,$keys,$type='empty')
    {
        if(!is_array($params))
            return false;
        foreach($keys as $key)
        {
            if($type=='empty')
            {
                if(empty($params[$key]))
                    return false;
            }elseif($type=='isset')
            {
                if(!isset($params[$key]))
                    return false;
            }else
                return false;
        }
        return true;
    }

    public function showMessage($msg, $url = '',$type=false)
    {
        $message = array();
        if ($type) {
            $message['type'] = 'success';
        } else {
            $message['type'] = 'error';
        }
        $message['msg'] = $msg;
        Yii::app()->user->setFlash('alertmsg', $message);
        if (!empty($url)) {
            $this->redirect($url);
        }
    }

    public function showSuccess($msg, $url = '')
    {
        $message = array();
        $message['type'] = 'success';
        $message['msg'] = $msg;
        Yii::app()->user->setFlash('alertmsg', $message);
        if (!empty($url)) {
            $this->redirect($url);
        }
    }
    public function showError($msg, $url = '')
    {
        $message = array();
        $message['type'] = 'error';
        $message['msg'] = $msg;
        Yii::app()->user->setFlash('alertmsg', $message);
        if (!empty($url)) {
            $this->redirect($url);
        }
    }

    public function getPost($name, $default = null)
    {
        return Yii::app()->request->getPost($name, $default);
    }

    public function getQuery($name, $default = null)
    {
        return Yii::app()->request->getQuery($name, $default);
    }

}
